<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class ClientRegisterForm extends CFormModel {

    public $firstname;
    public $lastname;
    public $street;
    public $street_no;
    public $city;
    public $pesel;
    public $birthday;
    public $email;
    public $phone;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('firstname,lastname,street,city,birthday,pesel,phone,email', 'required', 'message' => 'Pole wymagane'),
            array('birthday', 'checkdate'),
            array('email','email', 'message' => 'nie poprawny email'),
            array('pesel', 'checkpesel'),
            array('birthday', 'date', 'format' => 'yyyy-MM-dd', 'message' => 'Format daty to yyyy-MM-DD'),
            array('firstname,lastname', 'length', 'min' => 3, 'message' => 'Minimalna ilośc znaków to 3'),
            array('phone', 'length', 'min' => 9, 'message' => 'Nie poprawny numer telefonu'),
            array('phone', 'type', 'type' => 'integer', 'message' => 'Nie poprawny numer telefonu'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'firstname' => 'Imię',
            'lastname' => 'Nazwisko',
            'street' => 'Ulica',
            'street_no' => 'Numer lokalu',
            'city' => 'Miejscowość',
            'birthday' => 'Data urodzenia',
            'pesel' => 'Pesel',
            'phone' => 'Telefon kontaktowy',
            'email' => 'Adres E-mail'
        );
    }

    public function checkdate($attribute, $params) {
        //if (!$this->hasErrors()) {
        $bd = $this->birthday;
        // $birthday can be UNIX_TIMESTAMP or just a string-date.
        if (is_string($bd)) {
            $bd = strtotime($bd);
        }

        // check
        // 31536000 is the number of seconds in a 365 days year.
        if (time() - $bd < 18 * 31536000) {
            var_dump('zle');
            $this->addError('birthday', 'Trzeba mieć ukończone 18 lat');
        }
        // }
    }

    public function checkpesel($attribute, $params) {
        //if (!$this->hasErrors()) {
        //    $this->addError('password', 'Niepoprawny pesel');
        //}
    }
    
     public function addClient(){
        return Yii::app()->db->createCommand("SELECT createclientfun('".$this->firstname."','".$this->lastname."','".strtolower($this->lastname).$this->pesel."','".md5(strtolower($this->lastname).$this->pesel)."','".$this->street."','".$this->city."','".$this->street_no."','".$this->pesel."','".$this->birthday."','".$this->phone."','".$this->email."')")->execute();
    }

}
